package cn.cq.admin.service.impl;

import cn.cq.common.BaseConst;
import cn.cq.admin.entity.vo.ResourceVo;
import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cq.admin.entity.model.TResource;
import cn.cq.admin.service.TResourceService;
import cn.cq.admin.mapper.TResourceMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
* @author dicraft
* @description 针对表【t_source(系统资源)】的数据库操作Service实现
*/
@Service
public class TResourceServiceImpl extends ServiceImpl<TResourceMapper, TResource>
    implements TResourceService {

    @Override
    public SaResult doSave(TResource resource) {
        if (ObjectUtils.isEmpty(resource.getPid())){
            resource.setPid(BaseConst.PARENT_NODE);
            if(resource.getType() == BaseConst.SOURCE_OF_BUTTON){
                return SaResult.error("按钮资源不能作为父节点！");
            }
        }
        save(resource);
        return SaResult.ok("资源保存成功！");
    }

    @Override
    public SaResult doModify(TResource resource) {
        if(resource.getId().equals(resource.getPid())){
            return SaResult.error("父节点不能为自身！");
        }
        if(updateById(resource)){
            return SaResult.ok("资源修改成功！");
        }
        return SaResult.error("资源修改失败！");
    }

    @Override
    public SaResult doRemove(List<Long> ids) {
        if(removeByIds(ids)){
            return SaResult.ok("资源删除成功！");
        }
        return SaResult.error("资源删除失败！");
    }

    @Override
    public SaResult doList(boolean withOutBtn) {
        QueryWrapper<TResource> resourceQw = new QueryWrapper<>();
        List<TResource> resourceList = list(resourceQw);
        //获取父节点
        List<ResourceVo> parentList = resourceList.stream()
                .filter(x ->
                    x.getPid() == BaseConst.PARENT_NODE
                    && x.getType() == BaseConst.SOURCE_OF_VIEW
                )
                .map(resource -> convertToResourceVo(resource))
                .collect(java.util.stream.Collectors.toList());
        //获取子节点
        for (ResourceVo parentVo : parentList) {
            getChildren(parentVo,resourceList,withOutBtn);
        }
        return SaResult.data(parentList);
    }

    /**
     * @author dicraft
     * @description 获取子节点
     * @param resourceVo
     * @param resourceList
     */
    private void getChildren(ResourceVo resourceVo, List<TResource> resourceList,boolean withOutBtn) {
        List<ResourceVo> children = resourceList.stream()
                .filter(x -> {
                    if(withOutBtn){
                        return x.getPid().equals(Long.valueOf(resourceVo.getId()))
                                && x.getType() == BaseConst.SOURCE_OF_VIEW;
                    }else{
                        return x.getPid().equals(Long.valueOf(resourceVo.getId()));
                    }
                })
                .map(resource -> convertToResourceVo(resource))
                .collect(java.util.stream.Collectors.toList());
        resourceVo.setChildren(children);
        children.forEach(x -> getChildren(x, resourceList,withOutBtn));
    }

    /**
     * @description 将实体类装换为vo类
     * @author dicraft
     * @param resource 资源实体
     * @return cn.cq.admin.entity.vo.ResourceVo
     */
    private ResourceVo convertToResourceVo(TResource resource){
        ResourceVo resourceVo = new ResourceVo();
        BeanUtils.copyProperties(resource, resourceVo);
        resourceVo.setId(String.valueOf(resource.getId()));
        resourceVo.setPid(String.valueOf(resource.getPid()));
        return resourceVo;
    }

}




